package com.resifind.www.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.resifind.www.constant.ImgPaths;
import com.resifind.www.constant.UserTypes;
import com.resifind.www.exception.BaseException;
import com.resifind.www.exception.Resp;
import com.resifind.www.po.*;
import com.resifind.www.service.*;
import com.resifind.www.service.impl.*;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author 36069
 */
@SuppressWarnings({"unused", "RedundantThrows"})
@WebServlet("/client/*")
@MultipartConfig
public class ClientServlet extends BaseServlet {
    private final UserService userService = new UserServiceImpl();
    private final ClientService clientService = new ClientServiceImpl();
    private final MerchantService merchantService = new MerchantServiceImpl();
    private final OrderService orderService = new OrderServiceImpl();
    private final BlacklistFavourService blacklistFavourService = new BlacklistFavourServiceImpl();

    /**
     * 查询所有符合条件的商家信息
     */
    public void queryMerchant(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        // 接收页码参数
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        // 接收查询条件参数
        String params = req.getReader().readLine();
        JSONObject root = JSONObject.parseObject(params);
        int clientId = root.getInteger("clientId");
        String tradeName = root.getString("tradeName");
        try {
            Resp<PageBean<Merchant>> listResp = merchantService.queryByPageAndCondition(currentPage, pageSize, tradeName, clientId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 查询订单信息
     */
    public void queryOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        User user = JSON.parseObject(params, User.class);
        try {
            Resp<List<Order>> listResp = orderService.queryOrderByUserId(user.getId(), user.getUserType());
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 下单
     */
    public void orderRoom(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        int clientId = root.getObject("clientId", int.class);
        int roomId = root.getObject("roomId", int.class);
        int roomNumberId = root.getObject("roomNumberId", int.class);
        try {
            Resp<String> listResp = orderService.orderRoom(clientId, roomId, roomNumberId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 取消订单
     */
    public void cancelOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        int orderId = JSON.parseObject(params, int.class);
        try {
            Resp<String> listResp = orderService.cancelOrder(orderId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 拉黑商户
     */
    public void blackMerchant(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        int clientId = root.getInteger("clientId");
        int merchantId = root.getInteger("merchantId");
        try {
            Resp<String> listResp = blacklistFavourService.blackMerchant(clientId, merchantId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 收藏商户
     *
     * @throws IOException 异常
     */
    public void favourMerchant(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        int clientId = root.getInteger("clientId");
        int merchantId = root.getInteger("merchantId");
        try {
            Resp<String> listResp = blacklistFavourService.favourMerchant(clientId, merchantId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 修改客户信息
     */
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Client client = JSON.parseObject(params, Client.class);
        try {
            Resp<String> listResp = clientService.updateClient(client);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 查询收藏的商户
     */
    public void fetchFavour(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer clientId = JSON.parseObject(params, int.class);
        try {
            Resp<List<Merchant>> listResp = blacklistFavourService.queryFavourMerchant(clientId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 取消收藏的商户
     */
    public void cancelFavour(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        Integer clientId = root.getInteger("clientId");
        Integer merchantId = root.getInteger("merchantId");
        try {
            Resp<String> listResp = blacklistFavourService.cancelFavour(clientId, merchantId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 查询黑名单的商户
     */
    public void fetchBlackList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer clientId = JSON.parseObject(params, int.class);
        try {
            Resp<List<Merchant>> listResp = blacklistFavourService.queryBlackList(clientId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 取消黑名单的商户
     */
    public void cancelBlack(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        Integer clientId = root.getInteger("clientId");
        List<Integer> merchantIds = root.getJSONArray("merchantIds").toJavaList(int.class);
        try {
            Resp<String> listResp = blacklistFavourService.cancelBlack(clientId, merchantIds, UserTypes.CLIENT);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 实名认证
     */
    public void realName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Client client = JSON.parseObject(params, Client.class);
        try {
            Resp<String> listResp = clientService.updateClient(client);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 充值
     */
    public void recharge(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        JSONObject root = JSON.parseObject(params);
        Integer clientId = root.getInteger("clientId");
        Double amount = root.getDouble("amount");
        try {
            Resp<String> listResp = clientService.recharge(clientId, amount);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 删除订单
     */
    public void deleteOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer orderId = JSON.parseObject(params, int.class);
        try {
            Resp<String> listResp = orderService.deleteOrder(orderId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 支付订单
     */
    public void payOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer orderId = JSON.parseObject(params, int.class);
        try {
            Resp<String> listResp = orderService.payOrder(orderId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 申请退款
     */
    public void applyRefund(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer orderId = JSON.parseObject(params, int.class);
        try {
            Resp<String> listResp = orderService.applyRefund(orderId);
            resp.getWriter().write(JSON.toJSONString(listResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSON.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 查询签到记录
     */
    public void querySignIn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer clientId = JSON.parseObject(params, int.class);
        Resp<SignInBean> signInBeanResp = clientService.querySignIn(clientId);
        resp.getWriter().write(JSONObject.toJSONString(signInBeanResp));
    }

    /**
     * 签到
     */
    public void signIn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String params = req.getReader().readLine();
        Integer clientId = JSON.parseObject(params, int.class);
        try {
            Resp<String> stringResp = clientService.signIn(clientId);
            resp.getWriter().write(JSONObject.toJSONString(stringResp));
        } catch (BaseException e) {
            resp.getWriter().write(JSONObject.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }

    /**
     * 头像上传
     */
    public void uploadAvatar(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Part file = req.getPart("file");
        String param = req.getParameter("clientId");
        int clientId = Integer.parseInt(param);
        String fileName = file.getSubmittedFileName();
        // 获取文件输入流
        InputStream inputStream = file.getInputStream();
        try {
            Resp<String> avatarResp = clientService.updateAvatar(clientId, fileName, inputStream);
            //构建头像访问路径
            avatarResp.setData(ImgPaths.AVATAR_WEB_PATH + avatarResp.getData());
            resp.getWriter().write(JSONObject.toJSONString(avatarResp));
        } catch (BaseException e) {
            //响应抛出的异常信息
            resp.getWriter().write(JSONObject.toJSONString(Resp.error(e.getCode(), e.getMsg())));
        }
    }
}
